import { postAction } from '@/api/manage'
import { UtilsMixin } from '@/mixins/UtilsMixin'
export const UserMixin = {
  mixins: [ UtilsMixin],
  data() {
    return {
      urls: {
        personList: '/person/info/personList',
      },
      personMap: [],
      realNameMap: [],
      personList: [],
      selectUserTimeOut: null,
      selectUserKeyWord: null,
    }
  },
  methods: {
    fetchUserList(value) {
      if (!value) {
        return
      }
      if (this.selectUserTimeOut) {
        window.clearTimeout(this.selectUserTimeOut)
      }
      this.selectUserTimeOut = window.setTimeout(this.toFetchUserList, 800)
      this.selectUserKeyWord = value
    },
    async toFetchUserList() {
      let param = {}
      param.keyword = this.selectUserKeyWord
      param.pageSize = 100
      let res = await postAction(this.urls.personList, param)
      if (res.success) {
        this.personList = res.result.records
        this.personList.forEach((p) => {
          this.$set(this.personMap, p.username, p)
        })
      }
    },
    selectUserList(selected, obj, mutil, mapList, index = -1) {
      let vm = this
      let curentList = []
      let currentObj = vm[obj]
      if (index > -1) {
        currentObj = vm[obj][index]
      }
      if (currentObj && currentObj[mapList[0].to]) {
        let length = currentObj[mapList[0].to].split(',').length
        for (let i = 0; i < length; i++) {
          let current = curentList[i] || {}
          for (let map of mapList) {
            let values = currentObj[map.to].split(',')
            current[map.from] = values[i]
          }
          curentList[i] = current
        }
      }
      curentList.forEach((p) => {
        vm.$set(vm.personMap, p.username, p)
        vm.$set(vm.personMap, p.realname, p)
      })
      if (!selected || selected.length <= 0) {
        mapList.forEach((map) => {
          currentObj[map.to] = ''
        })
      } else {
        let selectMap = {}
        if (Array.isArray(selected)) {
          for (let s of selected) {
            if (s && s.indexOf('_') > -1) {
              s = s.split('_')[0]
            }
            for (let map of mapList) {
              if (vm.personMap[s]) {
                if (selectMap[map.to]) {
                  if (selectMap[map.to].indexOf(vm.personMap[s][map.from]) < 0) {
                    selectMap[map.to] = [...selectMap[map.to], vm.personMap[s][map.from]]
                  }
                } else {
                  selectMap[map.to] = [vm.personMap[s][map.from]]
                }
              }
            }
          }
        } else {
          if (selected && selected.indexOf('_') > -1) {
            selected = selected.split('_')[0]
          }
          mapList.forEach((map) => {
            selectMap[map.to] = [vm.personMap[selected][map.from]]
          })
        }
        for (let k in selectMap) {
          currentObj[k] = selectMap[k].join(',')
        }
      }

      if (index > -1) {
        vm[obj][index] = currentObj
      } else {
        vm[obj] = currentObj
      }
      vm.$forceUpdate()
    },
  },
}
